class Handler:
    """抽象处理者类"""

    def __init__(self, successor=None):
        self._successor = successor

    def handle(self, request):
        raise NotImplementedError("Subclasses must implement this method")


class LowLevelHandler(Handler):
    """低级别的问题处理者"""

    def handle(self, request):
        if request < 10:
            print(f"LowLevelHandler: Handling request {request}")
        elif self._successor is not None:
            self._successor.handle(request)


class MidLevelHandler(Handler):
    """中级别的问题处理者"""

    def handle(self, request):
        if 10 <= request < 20:
            print(f"MidLevelHandler: Handling request {request}")
        elif self._successor is not None:
            self._successor.handle(request)


class HighLevelHandler(Handler):
    """高级别的问题处理者"""

    def handle(self, request):
        if request >= 20:
            print(f"HighLevelHandler: Handling request {request}")
        elif self._successor is not None:
            self._successor.handle(request)


# 创建责任链
low_level = LowLevelHandler()
mid_level = MidLevelHandler(low_level)
high_level = HighLevelHandler(mid_level)

# 发送请求
requests = [5, 15, 25, 2]
for req in requests:
    print(f"Processing request {req}")
    high_level.handle(req)

"""
Processing request 5
LowLevelHandler: Handling request 5
Processing request 15
MidLevelHandler: Handling request 15
Processing request 25
HighLevelHandler: Handling request 25
Processing request 2
LowLevelHandler: Handling request 2
"""